//
//  PictureView.m
//  UI06_UIscrollView_UIPageControl
//
//  Created by aaaaaa on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PictureView.h"
#import "CustomUIScrollView.h"

@interface PictureView ()<UIScrollViewDelegate>

@property (nonatomic, retain) CustomUIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;

@end


@implementation PictureView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc
{
    [_scrollView release];
    [_pageControl release];
    [_imageArray release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        /**在初始化的时候对所有属性进行初始化**/
        self.imageArray = [NSArray array];
        
        
        [self createSubViews];
    }
    return self;
}

//创建子视图
- (void)createSubViews{
    //创建scrollView,并添加到父视图上
    self.scrollView = [[CustomUIScrollView alloc] initWithFrame:self.bounds];
    self.scrollView.backgroundColor = [UIColor grayColor];
    
    //设置偏移量
    self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);
    
    [self addSubview:_scrollView];
    [self.scrollView release];
    self.scrollView.delegate = self;
    

    
    //创建pageControl,并添加到父视图上
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) * 0.9, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) * 0.1)];
//     self.pageControl.backgroundColor = [UIColor orangeColor];
    [self addSubview: _pageControl];
    [self.pageControl release];
    [ self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
}


/**pagecontrol点击方法**/
- (void)pageAction:(UIPageControl *)pageControl{
    /**
     * 重点：scrollView的偏移量
     * x:代表的是水平方向的偏移量，y:代表的是垂直方向的偏移量，它们都是相当于滚动范围的原点来说的
     */
//    self.scrollView.contentOffset = CGPointMake((pageControl.currentPage + 1)* CGRectGetWidth(self.bounds), 0);
    
    [self.scrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * CGRectGetWidth(self.bounds), 0) animated:YES];
}

/*重写setter方法*/
- (void)setImageArray:(NSArray<UIImage *> *)imageArray{
    if (_imageArray != imageArray) {
        [_imageArray release];
        _imageArray = [imageArray retain];
    }
    //添加图片
    [self.scrollView addPictures:_imageArray];
    
    self.pageControl.numberOfPages = _imageArray.count - 2;
}


/*UIScrollView的代理方法*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    /**当偏移量大于等于最后一张图片的位置时，将scrollView的偏移量改为实际展示的第一张图片的位置*/
    if (scrollView.contentOffset.x >= (self.imageArray.count - 1) * scrollView.bounds.size.width) {
        scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
    } else if (scrollView.contentOffset.x <= 0) {
        scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width * self.imageArray.count - 2, 0);
    }
    
    
    self.pageControl.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width - 1;
    
    
    for (CustomUIScrollView *scroll in scrollView.subviews) {
        scroll.zoomScale = 1.0;
    }
    
    
}


/*放大缩小*/

//- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
//    return scrollView.subviews.firstObject;
//}







@end
